﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StarTrek.Ship;
using StarTrek.Space;
using StarTrek.Weapon;
using StarTrek.World;

namespace StarTrek
{
    internal enum CaptianKnowlageQuanta
    {
        Noob,
        Resonable,
        Proficent,
        Experianced
    }
    internal enum CaptianAgressionQuanta
    {
        Peaceful,
        Netural,
        Agressive,
        WarManianc
    }

    class EnemyCaptian
    {
        private CaptianKnowlageQuanta smarts;
        private CaptianAgressionQuanta hate;
        public EnemyCaptian()
        {
            Random rnd = new Random();
            smarts = (CaptianKnowlageQuanta)rnd.Next(0, 3);
            hate = (CaptianAgressionQuanta)rnd.Next(0, 3);
        }

        /// <summary>
        /// the brains behind the command chair.
        /// </summary>
        /// <param name="Vessel">the vessel this ai is in command of</param>
        /// <returns>True only if the Palyers ship is damaged</returns>
        public bool PerformShipAction(iShip Vessel)
        {
            bool rVal = false;
            Galaxy theWorld = Galaxy.Instance;
            //Collect Facts
            float goodGuyDist = theWorld.CalculateDistance(Vessel.CurrentLocation, theWorld.CurrentShip.CurrentLocation);
            bool goodGuyLocal = Vessel.CurrentLocation.Parent.Contains(theWorld.CurrentShip.CurrentLocation);
            iWeapon bestChoice = ChooseWeapon(goodGuyDist, Vessel.PowerUnit, Vessel.GetWeapons());
            Random rnd = new Random();

            float maxE = Vessel.PowerUnit;

            if (smarts >= CaptianKnowlageQuanta.Resonable)
                maxE = maxE / 3;


            //Perform action
            if (goodGuyLocal && hate >= CaptianAgressionQuanta.Netural)
            {
                //True only if damaged.
                rVal = (null != Vessel.Offense(theWorld.CurrentShip, bestChoice, rnd.Next(1, (int)Math.Floor(maxE))));
            }

            //Perform action
            if (!rVal && goodGuyDist < 11)
            {
                iSpaceItem s = Vessel.CurrentLocation.Parent.GetOccupyableLocation(); //Get random location

                //if (smarts >=CaptianKnowlageQuanta.Resonable)
                //       Fly closer, however, this does not exist in this framework

                Vessel.Travel(s);
            }

            return rVal;
        }

        private iWeapon ChooseWeapon(float dist, float localE, List<iWeapon> Choices)
        {
            List<iWeapon> items = Choices.FindAll((w) => w.isOffensive && w.isFunctional);

            if (smarts >= CaptianKnowlageQuanta.Proficent && dist > 4)
            {
                items = items.FindAll((w) => !w.NeedsEnegery);
            }


            return items.First();

        }
    }
}
